<template>
    <div class="con">
        <h1>短信登录</h1>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="手机号" prop="password" >
                <el-input type="password" v-model="phoneNumber" autocomplete="off"></el-input>
                <span @click="sendSms" v-text="msg" class="captcha-svg" style="color:#888; font-weight:bolder;45px; text-decoration:underline; user-select:none;"></span>
            </el-form-item>
            <el-form-item label="验证码" prop="captcha">
                <el-input v-model="code" type="text"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submit">提交</el-button>
                <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import * as api from "@/api/users"
// 校验验证码
    var validateCaptcha = (rule, value, callback) => {
    // 至少5位数

    var reg =  /^\d{5}$/;

    if(reg.test(value)){
        callback()
    }else{
        callback('验证码不能为空白');
    }
    
    };
   
    // 校验手机号
    var validatePassword = (rule, value, callback) => {
    
    var reg3 = /^\d{11}$/

    
    if(reg3.test(value)){
        callback()
    }else{
        callback('请输入正确的手机号');
    }
    
    };
  export default {
    props:{
       storageUserInfo: {
            type: Function,
            require: true
        }
    },
    data() {
      return {
        msg:`点击发送验证码`,
        flag:true,//一个开关,
        phoneNumber:'',
        code: '',
        ruleForm: {
          password: '',
          captcha: '',
        },
        rules: {
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          captcha: [
            { validator: validateCaptcha, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      async sendSms(){
        if(this.flag){
          this.flag=false;//关掉
          //调用接口，获取手机验证码
          let res=await api.getSmsCode(this.phoneNumber)
          // console.log(res);
          if(res.data.state){
            this.$message({
              type:"success",
              message:"短信发送成功"
            })
          }else{
            this.$message.error(res.data.msg)
          }
          let delay = 5
          this.msg=`${delay}秒后重新发送`
          this.interval = setInterval(()=>{
            delay--
            this.msg=`${delay}秒后重新发送`
            if(delay==0){
              this.msg="点击发送验证码"
              clearInterval(this.interval)
              //将开关打开
              this.flag=true;
            }
          },1000)
        }
        },
      async submit(){
        let res=await api.smsLogin(this.code);
        // console.log(res);
        //调用父组件的方法，储存用户信息
        this.storageUserInfo(res.data)
        //跳转页面
        this.$router.push("/")
      }
        
    }
  }
</script>

<style  lang="less"  scoped>
body{
    padding:0;
    margin:0 !important;
}
.container{
    position:relative;
    overflow:hidden;
}

/deep/.el-button{
    width:260px;
    margin-left:-80px;
    margin-top:60px;
}  
/deep/.el-form-item__label{
  text-align:center;
  color:#fff
}

 /deep/ .captcha {
            width: 100px;
            vertical-align: top;

            .el-input__inner {
                width: 100%;
                height: 45px;
                border-radius: 5px 0 0 5px;
                border: 0;
                font-size: 18px;
            }
 }
.captcha-svg {
          background: #fff;
          display: inline-block;
          height: 45px;
          width: 150px;
          text-align: center;
          cursor: pointer;
          border: 0;
          border-radius: 0px 5px 5px 0;
      }
form button{
    width:180px;
}  
</style>